package norbert.flickr.controls {
	import mx.controls.Image;
	import norbert.flickr.api.Url;
	import mx.logging.ILogger;
	import mx.logging.Log;
	import flash.utils.getQualifiedClassName;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import mx.managers.PopUpManager;
	import mx.effects.Fade;

	public class PhotoImage extends Image {
		private static var logger: ILogger = Log.getLogger("norbert.flickr.controls.PhotoImage");
		
		/* PROPERTIES */
		
		private var _size: String = null;
		[Bindable]
		[Inspectable(category="General", enumeration="s,t,m,b")]
		public function get size(): String {
			return _size;
		}
		public function set size(value: String): void {
			_size = value;
			invalidateProperties();
		}
		
		private var _photo: XML = null;
		[Bindable]
		public function get photo(): XML {
			return _photo;
		}
		public function set photo(value: XML): void {
			_photo = value;
			invalidateProperties();
		}
		
		[Embed(source="/../resources/indicator-flickr.gif")]
		private var iconIndicatorFlickr: Class;
		
		private var indicator: Image;
		
		/* CONSTRUCTOR */
		
		public function PhotoImage() {
			/* FIXME betöltődés jelzése
			addEventListener(Event.OPEN, onOpen);
			addEventListener(Event.COMPLETE, onComplete);
			addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			*/
			
			var fadeIn: Fade = new Fade();
			fadeIn.duration = 500;
			setStyle("completeEffect", fadeIn);
		}
		
		/* EVENT HANDLERS */
		
		private function onOpen(event: Event): void {
			indicator.move(5, 5);
			PopUpManager.addPopUp(indicator, this);
		}

		private function onComplete(event: Event): void {
			PopUpManager.removePopUp(indicator);
		}

		private function onIOError(event: IOErrorEvent): void {
			PopUpManager.removePopUp(indicator);
		}
		
		/* METHODS */
		
		override protected function createChildren(): void {
			super.createChildren();
			
			if (!indicator) {
				indicator = new Image();
				indicator.source = iconIndicatorFlickr;
			}
		}
		
		override protected function commitProperties(): void {
			super.commitProperties();
			
			if (photo != null && size != null && size != "") {
				source = Url.getPhotoSourceUrl(photo, size);
			}
		}
	}
}
